package com.eascs.workflow.email.dao.impl;

import java.util.Date;

import org.springframework.stereotype.Repository;

import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.workflow.common.dao.BaseDao;
import com.eascs.workflow.common.entity.sys.SysEmail;
import com.eascs.workflow.common.entity.sys.SysMessage;
import com.eascs.workflow.common.entity.sys.WfMessage;
import com.eascs.workflow.common.entity.sys.WfWechat;
import com.eascs.workflow.email.dao.SysSendEmailOrMessageDao;

@Repository
public class SysSendEmailOrMessageDaoImpl extends BaseDao implements SysSendEmailOrMessageDao {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	@Override
	public int insertEmail(SysEmail email) {
		if (email == null) {
			logger.info("error,method:SysSendEmailOrMessageDaoImpl.insertEmail必要参数不能为空!");
			return 0;
		}
		return workflowJdbcSession.insert(email);
	}

	@Override
	public SysMessage insertMessage(SysMessage message) {
		if (message == null) {
			logger.info("error,method:SysSendEmailOrMessageDaoImpl.insertMessage必要参数不能为空!");
			return null;
		}
		int effectRow = workflowJdbcSession.insert(message);
		if (effectRow <= 0) {
			return null;
		}
		return message;
	}

	@Override
	public SysMessage getMessageById(String id) {
		return workflowJdbcSession.getById(SysMessage.class, id);
	}
	
    @Override
	public boolean updateMailStatus(String requestId) {
		return workflowJdbcSession.execute("update wf_email set status=1 , send_time=?  where request_id=? ", new Date(),requestId)>0;
	}
    
	@Override
	public boolean updateMailCounts(String requestId){
		String sql="update wf_email set counts=counts+1  where request_id=?" ;
		return  workflowJdbcSession.execute(sql, requestId)>0;
	}
	
    @Override
	public int insertNewMessage(WfMessage wfMessage) {
		if (wfMessage == null) {
			logger.info("error,method:SysSendEmailOrMessageDaoImpl.insertMessage必要参数不能为空!");
			return 0;
		}
		int effectRow = workflowJdbcSession.insert(wfMessage);
		return effectRow;
	}

	@Override
	public int insertWfWechat(WfWechat wfWechat) {
		int effectRow = workflowJdbcSession.insert(wfWechat);
		return effectRow;
	}
}
